from utils import *
import sys
import gtk
import config
import account
import database
import optparse
import friends
import wizard
import time
import information
import net
import gobject
import ui

   
   
class appMain:
    """Main application class"""
    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file("ui/main.glade")
        #display plugin left menu
        iconVbox = builder.get_object("iconVbox")
        for plugin in apps.pluginsList:
            button = gtk.Button()
            button.show()
            image = gtk.image_new_from_file(plugin.getIcon())
            image.show()
            button.set_image(image)
            button.connect("clicked",self.onIconClicked,plugin)
            iconVbox.pack_start(button,False,False)
        #get main window
        self.window = builder.get_object("mainWindow")
        #get app frame
        self._appWindow = builder.get_object("appScrolledWindow")
        builder.connect_signals(self)
        #test if we launch the wizard
        id = account.getMyAccount().getID()
        if not id:
            wiz = wizard.accountWizard()

    def onIconClicked(self,widget,data=None):
        if self._appWindow.get_child():
            self._appWindow.remove(self._appWindow.get_child())
        if data:
            self._appWindow.add_with_viewport(data.getFrame())
            
        
    def on_mainWindow_destroy(self,widget,data=None):
        #stop netserver
        net.getNetServer().stop()
        gtk.main_quit()
        
    def openAccountWindow(self,widget,data=None):
        app = ui.appAccount()
        app.dialog.run()
        
    def on_friendsmenuitem_activate(self,widget,data=None):
        app = ui.appFriend()
        app._dialog.run()
        
    def on_databasemenuitem_activate(self,widget,data=None):
        app = ui.appDatabase()
        app._dialog.run()
        
        
if __name__=="__main__":
    
    #parsing command line
    parser = optparse.OptionParser()
    parser.add_option("-c", "--config-file", dest="configFile", help="path to config file", metavar="FILE")
    (options, args) = parser.parse_args()
    if options.configFile:
        config.CONFIG_FILE = options.configFile
    else:
        config.CONFIG_FILE = 'conf.ini'
    #once config file loaded, we import apps
    import apps
    #start netserver
    gobject.threads_init() #must have unless netplug threads hangs
    server = net.netServer(account.getMyAccount().getNetPlugs())
    server.start()
    net.setDefaultNetServer(server)
    #launch GUI
    app = appMain()
    app.window.show()
    gtk.main()
